/***
 * 还需要引用
 * <!--引用百度地图API-->
 *   <script src="http://api.map.baidu.com/api?v=1.4" type="text/javascript"></script>
 * @param initLat  初始化纬度
 * @param initLng   初始化经度
 * @param inputId   需要赋值的input表单的id
 */
/*document.write('<script src="http://api.map.baidu.com/api?v=1.4" type="text/javascript"></script>');*/

var map;
function initMap(initLat,initLng,inputId){
    createMap(initLat,initLng);//创建地图
    setMapEvent(inputId);//设置地图事件
    addMapControl();//向地图添加控件
    addMapOverlay(initLat,initLng);//向地图添加覆盖物
}
function createMap(initLat,initLng){
    map = new BMap.Map("map");
    map.centerAndZoom(new BMap.Point(initLat,initLng),18);
}
function setMapEvent(inputId){
    map.enableScrollWheelZoom();
    map.enableKeyboard();
    map.enableDragging();
    map.enableDoubleClickZoom();
    map.addEventListener("click",function(e){
        //window.markers.position.lat=e.point.lat;
        //window.markers.position.lng=e.point.lng;
        //target.openInfoWindow(window);
        $(inputId).val(e.point.lat+","+e.point.lng);
        addMapOverlay(e.point.lat,e.point.lng);
    });
}
function addClickHandler(target,window){
    target.addEventListener("click",function(e){
        //window.markers.position.lat=e.point.lat;
        //window.markers.position.lng=e.point.lng;
        //target.openInfoWindow(window);
        alert(e.point.lat+"--"+e.point.lng);
        addMapOverlay(e.point.lat,e.point.lng);
    });
}
function addMapOverlay(latOjb,lngOjb){
    map.clearOverlays();
    var markers =
    {content:"我的备注",title:"我的地址位置",imageOffset: {width:0,height:3},position:{lat:latOjb,lng:lngOjb}};
    //for(var index = 0; index < markers.length; index++ ){
    var point = new BMap.Point(markers.position.lng,markers.position.lat);
    var marker = new BMap.Marker(point,{icon:new BMap.Icon("http://api.map.baidu.com/img/markers.png",new BMap.Size(20,25),{
        imageOffset: new BMap.Size(markers.imageOffset.width,markers.imageOffset.height)
    })});
    //var label = new BMap.Label(markers.title,{offset: new BMap.Size(25,5)});
    var opts = {
        width: 200,
        title: markers.title,
        enableMessage: false
    };
    var infoWindow = new BMap.InfoWindow(markers.content,opts);
    //marker.setLabel(label);
    //addClickHandler(marker,infoWindow);

    map.addOverlay(marker);
    //};
}
//向地图添加控件
function addMapControl(){
    var scaleControl = new BMap.ScaleControl({anchor:BMAP_ANCHOR_BOTTOM_LEFT});
    scaleControl.setUnit(BMAP_UNIT_IMPERIAL);
    map.addControl(scaleControl);
    var navControl = new BMap.NavigationControl({anchor:BMAP_ANCHOR_TOP_LEFT,type:BMAP_NAVIGATION_CONTROL_LARGE});
    map.addControl(navControl);
    var overviewControl = new BMap.OverviewMapControl({anchor:BMAP_ANCHOR_BOTTOM_RIGHT,isOpen:true});
    map.addControl(overviewControl);
}
